<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>会员等级</legend>
        <div class="layui-field-box">
            <div class="layui-card">

                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">是否启用</label>
                            <div class="layui-input-inline">
                                <select name="status" lay-verify="">
                                    <option value="">全部</option>
                                    <option value="0">未启用</option>
                                    <option value="1">已启用</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list layui-btn-normal" lay-submit lay-filter="LAY-app-contlist-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body higo-card">
                    <div style="padding-bottom: 15px;">
                        <button class="layui-btn layuiadmin-btn-useradmin layui-btn-normal layui-btn-sm" data-type="add">添加会员等级</button>
                    </div>
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="status">
                            <input type="checkbox" class="layui-btn-normal" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="sexDemo" {{ d.status == 1 ? 'checked' : '' }}>
                    </script>
                    <script type="text/html" id="table-content-list">
                        <a class="layui-btn layui-btn-normal layui-btn-xs operation">操作<i class="layui-icon layui-icon-triangle-d"></i></a>
                        <ul class="layui-nav-child layui-anim layui-anim-upbit" style="padding: 10px; top: 22px; min-width: inherit; position: absolute; display: none;">
                            <li>
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑等级</a>
                            </li>
                            <li>
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除等级</a>
                            </li>
                        </ul>
                    </script>
                </div>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$,
            form = layui.form,
            table = layui.table;

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                url: "{:url('User/user_grade')}",
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('User/user_grade')}",
            method: 'post',
            cols: [[
                    {field: 'name', title: '等级名称'},
                    {field: 'img', title: '等级图片', templet: '<div><img src="/uploads/{{d.img}}" /></div>'},
                    {field: 'grade', title: '等级', edit: 'test', templet: function (d){ return 'Level' + d.grade; }},
                    {field: 'discount', title: '享受折扣', edit: 'test'},
                    {field: 'status', title: '是否启用', templet: "#status"},
                    {field: 'explain', title: '等级说明'},
                    {title: '操作', width: 100, align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听修改
        table.on('edit(demo)', function(obj){
            var id = obj.data.id; //所在行的所有相关id
            var field = obj.field; //当前编辑的字段名
            var value = obj.value; //得到修改后的值
            var that = obj.tr.find('td[data-field=' + field + ']');
            if (isNaN(value)) return layer.msg('只能为数字');
            $.post("{:url('User/grade_field')}", {id:id,field:field,value:value}, function (e){
                if (e.code == 200){
                    layer.msg(e.data, {offset: ['35%', '40%']});
                    setTimeout(function(){
                        table.reload('demo');
                    }, 500);
                }
            }, 'json');
        });

        //监听开关
        form.on('switch(sexDemo)', function(obj){
            var id = this.value;
            var field = this.name;
            var open = obj.elem.checked ? 1 : 0;
            $.post("{:url('User/grade_open')}", {id:id,field:field,open:open}, function (e){
                if (e.code == 200){
                    layer.tips(e.data, obj.othis, {
                        tips: 3
                    });
                }
            }, 'json');
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;
            if(layEvent === 'edit'){ //编辑
                parent.layer.open({
                    type: 2,
                    title: '编辑会员等级',
                    content: '/admin/User/grade_operate/id/' + id,
                    maxmin: true,
                    area: ['500px', '500px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index) {
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            } else if(layEvent === 'del'){ //删除
                parent.layer.confirm('确定删除吗', function(index){
                    //向服务端发送删除指令
                    $.ajax({
                        type: "POST",
                        url: "{:url('User/grade_del')}",
                        data: {id:id},
                        dataType: "json",
                        success: function(e){
                            if (e.code == 200){
                                table.reload('demo');
                                parent.layer.msg(e.data);
                            }else{
                                parent.layer.msg(e.msg);
                            }
                        }
                    });
                });
            }
        });

        //事件
        var active = {
            add: function(){
                parent.layer.open({
                    type: 2,
                    title: '添加会员等级',
                    content: "{:url('User/grade_operate')}",
                    maxmin: true,
                    area: ['500px', '500px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index) {
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
